Skip to content

Comments

feat: add HostFunc option to customize host metric label#143

Merged
depado merged 1 commit intodepado:mainfrom
franciscocpg:feat/add-host-func
Feb 12, 2026
Merged

feat: add HostFunc option to customize host metric label#143
depado merged 1 commit intodepado:mainfrom
franciscocpg:feat/add-host-func

Conversation

@franciscocpg
Copy link
Contributor

Summary

  • Add HostFunc option following the same pattern as HandlerNameFunc and RequestPathFunc
  • Allow users to override the host value used in Prometheus metric labels (both counter and histogram) to reduce cardinality when the Host header varies (e.g. dynamic subdomains)
  • When not set, defaults to c.Request.Host (preserving current behavior)

Motivation

When services handle requests with dynamic subdomains (e.g. 12345.example.com), the host label in Prometheus metrics causes high cardinality. Currently there is no way to customize the host label without mutating c.Request.Host via external middleware before instrumentation.

This adds a HostFunc option that lets users normalize the host at the metric level:

p := ginprom.New(
    ginprom.HostFunc(func(c *gin.Context) string {
        return stripDynamicSubdomain(c.Request.Host)
    }),
)

Test plan

  • Added TestHostFunc following the same pattern as TestHandlerNameFunc
  • All existing tests pass

🤖 Generated with Claude Code

Allow users to override the host value used in Prometheus metric labels
(counter and histogram) to reduce cardinality when the Host header varies
(e.g. dynamic subdomains).

When not set, defaults to c.Request.Host (preserving current behavior).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Copy link
Owner

@depado depado left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@depado depado merged commit bd19a9a into depado:main Feb 12, 2026
4 checks passed
@depado
Copy link
Owner

depado commented Feb 12, 2026

Released in v1.8.3 👍 Thanks a lot for your contribution 😄

@franciscocpg franciscocpg deleted the feat/add-host-func branch February 13, 2026 11:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants